<!--
* description: 维修工时标准修改弹窗  2019
* author:xuliwen
* createdDate:2021-2-3
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="70%"
      @close="sendCode"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :style-type="$store.state.app.dialogType"
      />
      <div class="filter-container filter-params">
        <el-row :gutter="26">
          <component
            :is="comp.component"
            v-for="comp in tableComponents.filter((o) => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire + comp.compKey : comp.compKey"
            :key="comp.compKey"
            :validrule="comp.validrule"
            :code-field="comp.codeField"
            :text-field="comp.textField"
            :popups-key="comp.compKey"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            :disabled="comp.disabled"
            :is-require="comp.isRequire"
            :span="comp.span || 8"
            :options="comp.options"
            :is-mul="comp.isMul"
            :label-name="comp.labelName"
            :lookuptype="comp.lookuptype"
            :date-options-type="comp.dateOptionsType"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
          />
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from '@/components/wms/mixins/formMixins'
import api from '@/api/se/apiBaseData'
import request from '@/utils/request'
export default {
  components: {
    dialogHeader: () => import('@/components/dialogHeader') // 弹窗标题
  },
  // 组件混入对象
  mixins: [formMixins],
  data() {
    return {
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          icon: 'iconfont  icon-preservation',
          clickEvent: () => this.save(),
          text: '保存'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          icon: 'iconfont icon-preservatio',
          clickEvent: this.sendCode,
          text: '取消' // 取消
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: 'compKey1',
          labelName: '维修大类：',
          codeField: 'operatePartCode',
          textField: 'opratePartName',
          change: this.getSmallClass,
          component: () => import('@/components/wms/dropdownList/dropdownList'),
          options: [],
          type: 'dropdownList',
          isRequire: true,
          span: 8,
          isMul: false,
          isMust: true
        },
        {
          compKey: 'compKey2',
          labelName: '维修小类：',
          codeField: 'rrpairSmallKindCode',
          textField: 'rrpairSmallKindName',
          disabled: true,
          component: () => import('@/components/wms/dropdownList/dropdownList'),
          options: [],
          type: 'dropdownList',
          isRequire: true,
          span: 8,
          isMul: false,
          isMust: true
        },
        {
          compKey: 'compKey3',
          labelName: '工时编码：',
          codeField: 'wiCode',
          component: () => import('@/components/org/commonInput'),
          type: 'inputText',
          isMust: true,
          isRequire: true,
          span: 8
        },
        {
          compKey: 'compKey4',
          labelName: '维修内容：',
          codeField: 'repairContent',
          component: () => import('@/components/org/commonInput'),
          type: 'inputText',
          isMust: true,
          isRequire: true,
          span: 8
        },
        // {
        //   compKey: "compKey5",
        //   labelName: "工时来源：",
        //   disabled:true,
        //   codeField: "wiSource",
        //   component: () => import("@/components/org/commonInput"),
        //   type: "inputText",
        //   isMust: true,
        //   span: 8,
        // },
        {
          compKey: 'compKey6',
          labelName: '最大工时数：',
          codeField: 'wiMaxSaleQty',
          component: () => import('@/components/org/commonInput'),
          type: 'inputText',
          isMust: true,
          isRequire: true,
          validrule: {
            noRequire: false,
                isformat: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
          },
          span: 8
        },
        {
          compKey: 'compKey7',
          labelName: '状态：',
          codeField: 'isEnable',
          component: () => import('@/components/wms/dropdownList/dropdownList'),
          options: [
            { value: '1', label: '启用' },
            { value: '0', label: '禁用' }
          ],
          type: 'dropdownList',
          span: 8,
          isRequire: true,
          isMul: false,
          isMust: true
        },
        {
          compKey: 'compKey8',
          labelName: '维修类别：',
          codeField: 'repairType',
          textField: 'repairTypeName',
          options: [],
          component: () => import('@/components/wms/dropdownList/dropdownList'),
          type: 'dropdownList',
          isMust: true,
          isRequire: true,
          span: 8
        },
        {
          compKey: 'compKey9',
          labelName: '备注：',
          codeField: 'remark',
          component: () => import('@/components/org/commonInput'),
          type: 'inputText',
          isMust: true,
          span: 8
        }
      ],
      // 标题
      textMap: {
        edit: '维修工时标准维护编辑',
        add: '维修工时标准维护新增'
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        operatePartCode: '',
        opratePlaceName: '',
        repairContent: '',
        rrpairSmallKindCode: '',
        rrpairSmallKindName: '',
        wiCode: '',
        wiMaxSaleQty: '',
        isEnable: '',
        repairType: '',
        remark: '',
        wiId: ''
      }
    }
  },
  methods: {
    onCreated() {
      if (this.curPopupsVisible) {
        this.getOperatePart()
        this.getRepairType()
        this.getrrpairSmallKind(this.formField.operatePartCode)
      }
    },
    getSmallClass(val) {
      this.formField.rrpairSmallKindCode = ''
      this.getrrpairSmallKind(val)
    },
    getRepairType() {
      request({
        url: api.queryRepairType.APIUrl,
        method: 'post',
        data: {}
      }).then((res) => {
        res.rows.map((item) => {
          this.tableComponents[6].options.push({
            label: item.repairTypeName,
            value: item.repairTypeCode
          })
        })
      })
    },
    getOperatePart() {
      request({
        url: api.findOpratePlace.APIUrl,
        method: 'post',
        data: {}
      }).then((res) => {
        res.rows.map((item) => {
          this.tableComponents[0].options.push({
            label: item.opratePartName,
            value: item.operatePartCode
          })
        })
      })
    },
    getrrpairSmallKind(val) {
      if (val != '' && val != null && val != undefined) {
        this.tableComponents[1].options = []
        this.tableComponents[1].disabled = false
        request({
          url: api.findSmallOpratePlace.APIUrl,
          method: 'post',
          data: { operatePartCode: val }
        }).then((res) => {
          res.rows.map((item) => {
            this.tableComponents[1].options.push({
              label: item.rrpairSmallKindName,
              value: item.rrpairSmallKindCode
            })
          })
        })
      }
    },
    // 新增/编辑保存按钮
    save() {
      const that = this
      this.$utils.validataMoth(this, 'validpopup')
      if (this.valid) {
        if (that.popupsState === 'edit') {
          that.editSave()
        } else {
          that.addSave()
        }
      }
    },
    // 编辑保存
    editSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: api.updateWorkingHoursStandard,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: this.$t('sys.tips.esTip5') /* 保存成功*/,
            type: 'success',
            duration: 1000
          })
          this.curPopupsVisible = false
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          // 保存失败
          this.$message({
            message: '保存失败:' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
        // this.curPopupsVisible = false;
      })
    },
    // 新增保存
    addSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: api.saveWorkingHoursStandard,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1000
          })
          this.curPopupsVisible = false
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          // 保存失败
          this.$message({
            message: '添加失败:' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
        this.curPopupsVisible = false
      })
    }
  }
}
</script>
<style type="text/css" scoped>
.filter-params .el-col label .curName {
  width: 150px;
}
</style>
